/*     */ package com.zimbra.cs.service.account;
/*     */ 
/*     */ import com.google.common.base.Strings;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AccountConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.Element.Disposition;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.httpclient.URLUtil;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.io.UnsupportedEncodingException;
/*     */ import java.net.URLEncoder;
/*     */ import java.security.InvalidKeyException;
/*     */ import java.security.NoSuchAlgorithmException;
/*     */ import java.text.SimpleDateFormat;
/*     */ import java.util.Date;
/*     */ import java.util.Map;
/*     */ import java.util.TimeZone;
/*     */ import javax.crypto.Mac;
/*     */ import javax.crypto.spec.SecretKeySpec;
/*     */ import org.apache.commons.codec.binary.Base64;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class GetAccountInfo
/*     */   extends AccountDocumentHandler
/*     */ {
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  57 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*     */     
/*  59 */     Element a = request.getElement("account");
/*  60 */     String key = a.getAttribute("by");
/*  61 */     String value = a.getText();
/*     */     
/*  63 */     if (Strings.isNullOrEmpty(value)) {
/*  64 */       throw ServiceException.INVALID_REQUEST("no text specified for the account element", null);
/*     */     }
/*     */     
/*  67 */     Provisioning prov = Provisioning.getInstance();
/*  68 */     Account account = prov.get(Key.AccountBy.fromString(key), value, zsc.getAuthToken());
/*     */     
/*     */ 
/*  71 */     if (account == null) {
/*  72 */       throw ServiceException.PERM_DENIED("can not access account");
/*     */     }
/*  74 */     Element response = zsc.createElement(AccountConstants.GET_ACCOUNT_INFO_RESPONSE);
/*  75 */     response.addAttribute("name", account.getName(), Element.Disposition.CONTENT);
/*  76 */     response.addKeyValuePair("zimbraId", account.getId(), "attr", "name");
/*  77 */     response.addKeyValuePair("zimbraMailHost", account.getAttr("zimbraMailHost"), "attr", "name");
/*  78 */     response.addKeyValuePair("displayName", account.getAttr("displayName"), "attr", "name");
/*  79 */     addUrls(response, account);
/*  80 */     return response;
/*     */   }
/*     */   
/*     */   static void addUrls(Element response, Account account) throws ServiceException {
/*  84 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/*  86 */     Server server = prov.getServer(account);
/*  87 */     String hostname = server.getAttr("zimbraServiceHostname");
/*  88 */     Domain domain = prov.getDomain(account);
/*  89 */     if ((server != null) && (hostname != null)) {
/*  90 */       String httpSoap = URLUtil.getSoapPublicURL(server, domain, false);
/*  91 */       String httpsSoap = URLUtil.getSoapPublicURL(server, domain, true);
/*     */       
/*  93 */       if (httpSoap != null) {
/*  94 */         response.addAttribute("soapURL", httpSoap, Element.Disposition.CONTENT);
/*     */       }
/*  96 */       if ((httpsSoap != null) && (!httpsSoap.equalsIgnoreCase(httpSoap)))
/*     */       {
/*     */ 
/*     */ 
/* 100 */         response.addAttribute("soapURL", httpsSoap, Element.Disposition.CONTENT);
/*     */       }
/* 102 */       String pubUrl = URLUtil.getPublicURLForDomain(server, domain, "", true);
/* 103 */       if (pubUrl != null) {
/* 104 */         response.addAttribute("publicURL", pubUrl, Element.Disposition.CONTENT);
/*     */       }
/* 106 */       String changePasswordUrl = null;
/* 107 */       if (domain != null)
/* 108 */         changePasswordUrl = domain.getAttr("zimbraChangePasswordURL");
/* 109 */       if (changePasswordUrl != null) {
/* 110 */         response.addAttribute("changePasswordURL", changePasswordUrl, Element.Disposition.CONTENT);
/*     */       }
/*     */     }
/* 113 */     if (account.getBooleanAttr("zimbraFeatureSocialExternalEnabled", false)) {
/* 114 */       String clientID = account.getAttr("zimbraCommunityAPIClientID");
/* 115 */       if (clientID == null) {
/* 116 */         ZimbraLog.account.debug("Zimbra Community client ID is not properly configured. zimbraCommunityAPIClientID cannot be empty.");
/*     */       }
/* 118 */       String clientSecret = account.getAttr("zimbraCommunityAPIClientSecret");
/* 119 */       if (clientSecret == null) {
/* 120 */         ZimbraLog.account.debug("Zimbra Community client secret is not properly configured. zimbraCommunityAPIClientSecret cannot be empty.");
/*     */       }
/* 122 */       String nameAttribute = account.getAttr("zimbraCommunityUsernameMapping");
/* 123 */       if (nameAttribute == null) {
/* 124 */         ZimbraLog.account.debug("Zimbra Community name mapping is not properly configured. zimbraCommunityUsernameMapping cannot be empty");
/*     */       }
/* 126 */       String socialBaseURL = account.getAttr("zimbraCommunityBaseURL");
/* 127 */       if (socialBaseURL == null) {
/* 128 */         ZimbraLog.account.debug("Zimbra Community base URL is not properly configured. zimbraCommunityBaseURL cannot be empty");
/*     */       }
/* 130 */       else if (socialBaseURL.endsWith("/")) {
/* 131 */         socialBaseURL = socialBaseURL.substring(0, socialBaseURL.length() - 1);
/*     */       }
/*     */       
/* 134 */       String socialTabURL = account.getAttr("zimbraCommunityHomeURL");
/* 135 */       if (socialTabURL == null) {
/* 136 */         ZimbraLog.account.debug("Zimbra Community home URL is not properly configured. zimbraCommunityHomeURL cannot be empty");
/*     */       }
/* 138 */       else if (!socialTabURL.startsWith("/")) {
/* 139 */         socialTabURL = "/".concat(socialTabURL);
/*     */       }
/*     */       
/* 142 */       if ((clientID != null) && (clientSecret != null) && (nameAttribute != null) && (socialBaseURL != null) && (socialTabURL != null)) {
/*     */         try {
/* 144 */           Date today = new Date();
/* 145 */           SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
/* 146 */           formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
/* 147 */           Mac mac = Mac.getInstance("HmacSHA256");
/* 148 */           SecretKeySpec key = new SecretKeySpec(clientSecret.getBytes("UTF8"), "HmacSHA256");
/* 149 */           mac.init(key);
/* 150 */           byte[] rawHmac = mac.doFinal(String.format("%s%s%s%s", new Object[] { account.getUid(), formatter.format(today), socialBaseURL, socialTabURL }).getBytes("UTF8"));
/*     */           
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 157 */           String Base64Signature = Base64.encodeBase64String(rawHmac);
/*     */           
/* 159 */           String szURL = String.format("%s/api.ashx/v2/oauth/redirect?client_id=%s&username=%s&time_stamp=%s&redirect_uri=%s&signature=%s", new Object[] { socialBaseURL, URLEncoder.encode(clientID, "UTF8"), account.getAttr(nameAttribute), URLEncoder.encode(formatter.format(today), "UTF8"), URLEncoder.encode(socialBaseURL.concat(socialTabURL), "UTF8"), URLEncoder.encode(Base64Signature, "UTF8") });
/*     */           
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 166 */           response.addAttribute("communityURL", szURL, Element.Disposition.CONTENT);
/*     */         } catch (UnsupportedEncodingException|NoSuchAlgorithmException|InvalidKeyException e) {
/* 168 */           throw ServiceException.FAILURE("Failed to generate community URL", e);
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/account/GetAccountInfo.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */